Function.RegisterNamespace("System.IO");

System.IO.Directory=new function(){
    var _strategyManager;

    // Public Members
    this.Strategies;
    
    // ctor
    function Directory(){
        this.Strategies=_strategyManager=new System.Script.Strategy.StrategyManager();
    }
    Directory.apply(this,arguments);
    
    // IDirectoryStrategy Members
    this.Exists=function(path){
        if(path==undefined||!Object.IsType(Function,path.toString))throw new Error("System.IO.Directory.Exists: 'path' must be convertible to String.");
        path=path.toString();
        try{
            return _strategyManager.Get(this).Exists(path);
        }catch(e){
            throw new Error(String.Format("System.IO.Directory.Exists: {0}",e));
        }
    };
    
    this.GetFiles=function(path){
        if(path==undefined||!Object.IsType(Function,path.toString))throw new Error("System.IO.Directory.GetFiles: 'path' must be convertible to String.");
        path=path.toString();
        if(this.Exists(path)){
            try{
                return sort(_strategyManager.Get(this).GetFiles(path));
            }catch(e){
                throw new Error(String.Format("System.IO.Directory.GetFiles: {0}",e));
            }
        }
        return [];
    };
    
    this.GetDirectories=function(path){
        if(path==undefined||!Object.IsType(Function,path.toString))throw new Error("System.IO.Directory.GetDirectories: 'path' must be convertible to String.");
        path=path.toString();
        if(this.Exists(path)){
            try{
                return sort(_strategyManager.Get(this).GetDirectories(path));
            }catch(e){
                throw new Error(String.Format("System.IO.Directory.GetDirectories: {0}",e));
            }
        }
        return [];
    };

    // Private Methods
    function pathSorter(a,b){
        if(!a)return -1;
        if(!b)return 1;
        a=a.toLowerCase();
        b=b.toLowerCase();
        if(!a||a<b)return -1;
        if(!b||a>b)return 1;
        return 0;
    }

    function sort(paths){
        if(!Object.IsType(Array,paths))return paths;
        return paths.sort(pathSorter);
    }
};

System.IO.Directory.constructor.Implement(System.IO.DirectoryStrategy.IDirectoryStrategy,'System.IO.Directory');
